package com.mysoft.gfyx.workbench.controller;

import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.framework.sdk.system.api.SystemDataProvider;
import com.mysoft.gfyx.common.util.PermUtil;
import com.mysoft.gfyx.workbench.controller.dto.NavResponseDto;
import com.mysoft.gfyx.workbench.controller.dto.NavWidgetAttrDto;
import com.mysoft.gfyx.workbench.dao.entity.PermTenantProfit;
import com.mysoft.gfyx.workbench.service.ProfitService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@PubService(value = "/navDataApi", prefix = RequestPrefix.API)
@Tag(name = "导航控制器-API")
public class NavDataApiController {

    @Resource
    private SystemDataProvider systemDataProvider;

    @Autowired
    private PermUtil permUtil;

    @Autowired
    private ProfitService profitService;

    @PubAction(value = "/quickEntrance", method = RequestMethod.POST)
    public NavResponseDto quickEntrance() {
        NavResponseDto responseData = new NavResponseDto();
        boolean enableMarketingPerm = false;
        boolean enableCluePerm = false;
        List<PermTenantProfit> list = profitService.getProfits();
        for (PermTenantProfit item : list) {
            if (item != null) {
                if (item.getProductCode().equals(ProfitService.Product.MarketReport.toString())) {
                    enableMarketingPerm = true;
                } else if (item.getProductCode().equals(ProfitService.Product.ClueRadarNationwide.toString()) ||
                        item.getProductCode().equals(ProfitService.Product.ClueRadarProvince.toString())) {
                    enableCluePerm = true;
                }
            }
        }

        // 市场大屏
        NavWidgetAttrDto navMarketDto = new NavWidgetAttrDto();
        navMarketDto.setKey("market-dashboard");
        navMarketDto.setName("市场大屏");
        if ((enableMarketingPerm)) {
            navMarketDto.setDisable(0);
        } else {
            navMarketDto.setDisable(1);
        }

        // 省市洞察
        NavWidgetAttrDto navInsightDto = new NavWidgetAttrDto();
        navInsightDto.setKey("area-insight");
        navInsightDto.setName("省市洞察");
        if ((enableMarketingPerm)) {
            navInsightDto.setDisable(0);
        } else {
            navInsightDto.setDisable(1);
        }

        // 找客户
        NavWidgetAttrDto navFindCustomerDto = new NavWidgetAttrDto();
        navFindCustomerDto.setKey("find-customer");
        navFindCustomerDto.setName("找客户");
        if (enableMarketingPerm || enableCluePerm) {
            navFindCustomerDto.setDisable(0);
        } else {
            navFindCustomerDto.setDisable(1);
        }

        // 找商机
        NavWidgetAttrDto navFindOppDto = new NavWidgetAttrDto();
        navFindOppDto.setKey("find-opportunity");
        navFindOppDto.setName("找商机");
        if (enableCluePerm) {
            navFindOppDto.setDisable(0);
        } else {
            navFindOppDto.setDisable(1);
        }

        // 客户管理
        NavWidgetAttrDto navCustomerManagementDto = new NavWidgetAttrDto();
        navCustomerManagementDto.setKey("customer-management");
        navCustomerManagementDto.setName("客户管理");
        if (enableMarketingPerm) {
            navCustomerManagementDto.setDisable(0);
        } else {
            navCustomerManagementDto.setDisable(1);
        }

        // 线索管理
        NavWidgetAttrDto navClueManagementDto = new NavWidgetAttrDto();
        navClueManagementDto.setKey("clue-management");
        navClueManagementDto.setName("线索管理");
        if (enableCluePerm) {
            navClueManagementDto.setDisable(0);
        } else {
            navClueManagementDto.setDisable(1);
        }

        responseData.setMarketDashboard(navMarketDto);
        responseData.setAreaInsight(navInsightDto);
        responseData.setFindCustomer(navFindCustomerDto);
        responseData.setFindOpp(navFindOppDto);
        responseData.setCustomerManagement(navCustomerManagementDto);
        responseData.setClueManagement(navClueManagementDto);

        // [{"key":"market-dashboard","name":"市场大屏","disable":0},{"key":"area-insight","name":"省市洞察","disable":0},{"key":"find-customer","name":"找客户","disable":0},{"key":"find-opportunity","name":"找商机","disable":0},{"key":"customer-management","name":"客户管理","disable":0},{"key":"opportunity--management","name":"商机管理","disable":0}]
        return responseData;
    }
}
